﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace SD.DataAccess
{
    public partial class Directorate
    {
        private readonly ServiceDBDataContext dataContext = new ServiceDBDataContext();

        /// <summary>
        /// add new Directorate to database
        /// </summary>
        /// <param name="newDirectorate"> an instance of Directorate class </param>
        /// <returns> true or false </returns>
        public bool AddDirectorate(Directorate newDirectorate)
        {
            try
            {
                dataContext.Directorates.InsertOnSubmit(newDirectorate);
                dataContext.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// edit new Directorate to database
        /// </summary>
        /// <param name="newDirectorate"> an instance of Directorate class </param>
        /// <returns> true or  false </returns>
        public bool AmendDirectorate(Directorate newDirectorate)
        {
            Directorate Directorate = dataContext.Directorates.FirstOrDefault(item => item.DirectorateId == newDirectorate.DirectorateId);
            if (Directorate != null)
            {
                //add entity
                Directorate.DirectorateName = newDirectorate.DirectorateName;
                Directorate.FullDescription = newDirectorate.FullDescription;
                Directorate.ShortDescription = newDirectorate.ShortDescription;
                Directorate.CityTown = newDirectorate.CityTown;
                Directorate.County = newDirectorate.County;
                Directorate.BusinessId = newDirectorate.BusinessId;
                Directorate.PhoneNumber = newDirectorate.PhoneNumber;
                Directorate.Fax = newDirectorate.Fax;
                Directorate.Email = newDirectorate.Email;
                Directorate.WebAddress = newDirectorate.WebAddress;
                Directorate.CharityNumber = newDirectorate.CharityNumber;
                Directorate.CompanyNumber = newDirectorate.CompanyNumber;
                Directorate.IsActive = newDirectorate.IsActive ;
                Directorate.AddressId = newDirectorate.AddressId;
                try
                {
                    dataContext.SubmitChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// edit status Directorate to database
        /// </summary>
        /// <param name="DirectorateId"> DirectorateId want to edit status </param>
        /// <param name="status"> status Directorate want to change </param>
        /// <returns> true or false </returns>
        public bool ChangeDirectorateStatus(int DirectorateId, bool status)
        {
            Directorate newDirectorate = dataContext.Directorates.FirstOrDefault(item => item.DirectorateId == DirectorateId);
            if (newDirectorate != null)
            {
                try
                {
                    newDirectorate.IsActive = status;
                    dataContext.SubmitChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// get list Directorate 
        /// </summary>
        /// <param name="status"> status want to select </param>
        /// <param name="filter"> type of filter </param>
	    /// <param name="sortBy"> type of sort </param>
	    /// <param name="sortType"> sort a->z or z->a </param>	
        /// <returns> list Directorate </returns>
        public List<StoredDirectorateResult> ListDirectorate(bool status, string filter, string sortBy, int sortType)
        {
            List<StoredDirectorateResult> newDirectorate = dataContext.StoredDirectorate(status, filter, sortBy, sortType).ToList();
            return newDirectorate;
        }

        /// <summary>
        /// get list Directorate of organazition
        /// </summary>
        /// <param name="status"> status want to select </param>
        /// <param name="filter"> type of filter </param>
        /// <param name="sortBy"> type of sort </param>
        /// <param name="sortType"> sort a->z or z->a </param>	
        /// <returns> list Directorate </returns>
        public List<StoredDirectorateOrganizationResult> ListDirectorateOfOrganization(int orgId, bool status, string filter, string sortBy, int sortType)
        {
            List<StoredDirectorateOrganizationResult> newDirectorate = dataContext.StoredDirectorateOrganization(orgId, status, filter, sortBy, sortType).ToList();
            return newDirectorate;
        }

        /// <summary>
        /// get Directorate selected
        /// </summary>
        /// <param name="DirectorateId"> DirectorateId selected </param>
        /// <returns> Directorate </returns>
        public DirectorateView GetDirectorate(int DirectorateId)
        {
            DirectorateView newDirectorate = dataContext.DirectorateViews.FirstOrDefault(item => item.DirectorateId == DirectorateId);
            return newDirectorate;
        }

        /// <summary>
        /// check name has already existed ?
        /// </summary>
        /// <param name="name"> name want to check </param>
        /// <returns> true or false </returns>
        public bool CheckNameDirectorate(string name)
        {
            Directorate newDirectorate = dataContext.Directorates.FirstOrDefault(item => item.DirectorateName == name);
            if (newDirectorate != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
